from utile.GetKeyWord import GetKeyWord
from utile.sendmethod import SendMethod


class MemberInterface:
    def __init__(self, url):
        self.url = url

    # 获取验证码
    def get_auth_code(self, telephone):
        method = "get"
        url = self.url + "/sso/getAuthCode"
        payload = {"telephone": telephone}
        return SendMethod.send_method(method=method, url=url, params=payload)

    # 提取验证码
    def get_verify_code(self, telephone):
        response = self.get_auth_code(telephone)
        return GetKeyWord.get_keyword(response, "data")

    # 注册用户
    def register(self, username, password, telephone):
        method = "post"
        url = self.url + "/sso/register"
        payload = {
            "username": username,
            "password": password,
            "telephone": telephone,
            "authCode": member.get_verify_code(telephone)
        }
        return SendMethod.send_method(method=method, url=url, data=payload)

    # 登录
    def login(self, username, password):
        method = "post"
        url = self.url + "/sso/login"
        payload = {
            "username": username,
            "password": password,
        }
        return SendMethod.send_method(url=url, method=method, data=payload)

    # 获取token
    def get_token(self, username, password):
        response = self.login(username, password)
        token = GetKeyWord.get_keyword(response, "token")
        return {"Authorization": f"Bearer {token}"}

    # 刷新token
    def refresh_token(self, username, password):
        method = "get"
        url = self.url + "/sso/refreshToken"
        token = self.get_token(username, password)
        response = SendMethod.send_method(method=method, url=url, headers=token)
        token = GetKeyWord.get_keyword(response, "token")
        return {"Authorization": f"Bearer {token}"}

    # 获取会员信息
    def get_member_info(self, username, password):
        method = "get"
        url = self.url + "/sso/info"
        token = self.get_token(username, password)
        return SendMethod.send_method(url=url, method=method, headers=token)

    # 会员修改密码
    def updata_password(self, payload):
        def update_password(self, payload):
            method = "post"
            url = self.url + "/sso/updatePassword"
            return SendMethod.send_method(url=url, method=method, data=payload)


if __name__ == '__main__':
    url = "http://47.108.206.100:8085"
    member = MemberInterface(url)
    # print(member.register("ymYM", 654321, "18871463422"))
    # 注册
    # print(member.get_auth_code("18871463422"))
    # print(member.get_verify_code("18871463422"))

    # 登录
    print(member.login("ymYM", 654321))
    # # 获取token
    # print(member.get_token("ymYM", 654321))
    # print(member.refresh_token("ymYM", 654321))

    # 获取会员信息
    # print(member.get_member_info("ymYM", 654321))

    # 修改密码
    # payload = {"authCode": member.get_verify_code(13533336789), "password": "1234", "telephone": "13533336789"}
    # print(member.updata_password(payload))
    # print(member.update_password(payload))
